package com.mbc.common.manager;

import java.sql.SQLException;
import java.util.ArrayList;

import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.LevelApplicationItemDAO;
import com.mbc.common.dao.RoleDAO;
import com.mbc.common.data.RoleDataList;
import com.mbc.common.entities.LevelApplicationItem;
import com.mbc.common.entities.Role;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.exception.ForeignConflictException;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.utils.KeyGenerator;

public class RoleMgr extends AbstractMgr {
	
	RoleDAO roleDAO;
	
	public RoleMgr()
	{}
	
	public RoleMgr(String sessionId)
	{
		this.sessionId = sessionId;
	}
	
	public void createRole(Role role)throws Exception
	{
		openConnection();
		try {
			dbManager.beginTransaction();
			roleDAO = CommonDAOFactory.getRoleDAO(dbManager.getConnection());
			LevelApplicationItemDAO l_LvlAppItemDAO = CommonDAOFactory.getLevelAppitemDAO(dbManager.getConnection());
			role.setRoleID(KeyGenerator.generateKey(dbManager.getConnection()));
			roleDAO.create(role);
			for (LevelApplicationItem l_lvlAppItem : role.getLevelAppItemList()) {
				l_lvlAppItem.setAutokey(KeyGenerator.generateKey(dbManager.getConnection()));
				l_lvlAppItem.setRoleId(role.getRoleID());
				l_LvlAppItemDAO.create(l_lvlAppItem);
			}
			
			dbManager.commitTransaction();
		} catch (SQLException e) {
			dbManager.rollBackTransaction();
			if(DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		}
		finally{
			dbManager.closeConnection();
		}
	}
	
	public void updateRole(Role role)throws Exception
	{
		openConnection();
		try {
			dbManager.beginTransaction();
			roleDAO = CommonDAOFactory.getRoleDAO(dbManager.getConnection());
			LevelApplicationItemDAO l_LvlAppItemDAO = CommonDAOFactory.getLevelAppitemDAO(dbManager.getConnection());
			
			l_LvlAppItemDAO.deleteByRoleId(role.getRoleID());
			roleDAO.update(role);
			
			for (LevelApplicationItem l_lvlAppItem : role.getLevelAppItemList()) {
				l_lvlAppItem.setAutokey(KeyGenerator.generateKey(dbManager.getConnection()));
				l_lvlAppItem.setRoleId(role.getRoleID());
				l_LvlAppItemDAO.create(l_lvlAppItem);
			}
			
			dbManager.commitTransaction();
		} catch (Exception e) {
			dbManager.rollBackTransaction();
			throw e;
		}
		finally{
			dbManager.closeConnection();
		}
	}
	
	public void deleteRole(long roleId)throws Exception{
		openConnection();
		try {
			dbManager.beginTransaction();
			roleDAO = CommonDAOFactory.getRoleDAO(dbManager.getConnection());
			LevelApplicationItemDAO l_LvlAppItemDAO = CommonDAOFactory.getLevelAppitemDAO(dbManager.getConnection());
			l_LvlAppItemDAO.deleteByRoleId(roleId);
			roleDAO.delete(roleId);
			dbManager.commitTransaction();
		} catch (SQLException e) {
			dbManager.rollBackTransaction();
			if(e.getErrorCode() == ForeignConflictException.FOREIGN_CONFLICT_ERROR_CODE)
				throw new ForeignConflictException(e);
			else
				throw e;
		}
		finally{
			dbManager.closeConnection();
		}
	}
	
	public Role findRole(long roleId)throws Exception{
		openConnection();
		Role l_Role = null;
		try {
			roleDAO = CommonDAOFactory.getRoleDAO(dbManager.getConnection());
			LevelApplicationItemDAO l_LvlAppItemDAO = CommonDAOFactory.getLevelAppitemDAO(dbManager.getConnection());
			l_Role = roleDAO.find(roleId);
			l_Role.setLevelAppItemList(l_LvlAppItemDAO.findByRoleId(roleId));
		} catch (Exception e) {
			throw e;
		}
		finally{
			dbManager.closeConnection();
		}
		return l_Role;
	}
	
	public ArrayList<Role> findAll()throws Exception
	{
		openConnection();
		try {
			roleDAO =  CommonDAOFactory.getRoleDAO(dbManager.getConnection());
			return roleDAO.findAll();
		} catch (Exception e) {
			throw e;
		}
		finally{
			dbManager.closeConnection();
		}
	}
	
	public RoleDataList getRoleList(CriteriaParams cri)throws Exception{
		openConnection();
		try {
			roleDAO = CommonDAOFactory.getRoleDAO(dbManager.getConnection());
			return roleDAO.getList(cri);
		} catch (Exception e) {
			throw e;
		}
		finally{
			dbManager.closeConnection();
		}
	}
	
	public ArrayList<LevelApplicationItem> getLevelAppItemByRoleId(long roleId)throws Exception{
		openConnection();
		try {
			LevelApplicationItemDAO l_lvlAppItemDAO = CommonDAOFactory.getLevelAppitemDAO(dbManager.getConnection());
			return l_lvlAppItemDAO.findByRoleId(roleId);
		} catch (Exception e) {
			throw e;
		} finally{
			dbManager.closeConnection();
		}
	}
}
